package com.ebt.m.customer.net.http;

import android.util.Log;

import com.ebt.m.jpush.PushTokenResult;
import com.ebt.m.customer.entity.Attachment;
import com.ebt.m.customer.entity.CompanyBean;
import com.ebt.m.customer.entity.PolicyBean;
import com.ebt.m.customer.entity.RiskBean;
import com.ebt.m.customer.net.json.ErrorJson;
import com.ebt.m.customer.net.json.PolicyAttachJson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by archer.qi on 2016/12/15.
 */
public class PolicyResponse {


    /**
     * 请求是否成功
     * <判断error标识></>
     *
     * @param result
     * @return
     */
    public static boolean isRequestSuccessful(String result) {
        if (result == null || "".equals(result)) {
            return false;
        }
        JSONObject resultObj = null;
        try {
            resultObj = new JSONObject(result);
            return !resultObj.has("error");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取错误信息
     *
     * @param result
     * @return
     */
    public static ErrorJson getErrorMessage(String result) {
        try {
            JSONObject resultObj = new JSONObject(result);
            JSONObject errorObj = resultObj.getJSONObject("error");
            ErrorJson errorJson = new ErrorJson();
            errorJson.code = errorObj.optString("code");
            errorJson.message = errorObj.optString("message");
            errorJson.type = errorObj.optString("type");
            return errorJson;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取附件列表 (CustomerPolicyAttachment_Create/CustomerPolicyAttachment_Delete)
     *
     * @param result
     * @return
     */
    public static PolicyAttachJson getResponse_AttachList(String result) {
        try {
            JSONObject resultObj = new JSONObject(result);
            JSONObject dataObj = resultObj.getJSONObject("data");
            PolicyAttachJson attachJson = new PolicyAttachJson();
            attachJson.policyId = dataObj.optString("policyId");
            List<Attachment> attachmentList = new ArrayList<>();
            JSONArray arr = dataObj.getJSONArray("listPolicyAttachment");
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);
                Attachment attachment = new Attachment();
                attachment.setId(obj.optString("id"));
                if (obj.has("attachmentName")) {
                    attachment.setName(obj.optString("attachmentName"));
                }
                if (obj.has("attachmentPath")) {
                    attachment.setUrl(obj.optString("attachmentPath"));
                }
                attachmentList.add(attachment);
            }
            attachJson.attachmentList = attachmentList;
            return attachJson;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取保单ID(CustomerPolicy_Create/CustomerPolicy_Delete/CustomerPolicy_Update)
     *
     * @param result
     * @return
     */
    public static String getResponse_PolicyId(String result) {
        try {
            JSONObject resultObj = new JSONObject(result);
            JSONObject dataObj = resultObj.getJSONObject("data");
            return dataObj.optString("policyId");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取保单列表( CustomerPolicyList)
     *
     * @param result
     * @return
     */
    public static List<PolicyBean> getResponse_PolicyList(String result) {
        Log.i("Policy", "->请求返回： " + result);
        try {
            JSONObject jsonObj = new JSONObject(result);
            JSONArray arr = jsonObj.optJSONArray("data");
            if (arr != null) {
                List<PolicyBean> policyList = new ArrayList<>();
                int length = arr.length();
                for (int i = 0; i < length; i++) {
                    JSONObject obj = arr.getJSONObject(i);
                    if (obj != null) {
                        PolicyBean policy = new PolicyBean();
                        policy.setPolicyId(obj.optString("uuid"));
                        policy.setPolicyNo(obj.optString("policyNumber"));
                        policy.setCustomerId(obj.optString("customerUUId"));
                        policy.setInsureNo(obj.optString("applicationNo"));
                        policy.setPermium(obj.optDouble("premium"));
                        policy.setCoverage(obj.optDouble("coverage"));
                        policy.setPayPeriodByCode(obj.optInt("payPeriod"));
                        policy.setEffectDate(obj.optString("effectDateTime"));
                        policy.setAcceptDate(obj.optString("receiveDate"));
                        policy.setReceiptDate(obj.optString("receiptDate"));
                        policy.setPolicyStatus(obj.optInt("policyStatus"), obj.optString("policyStatusValue"));

                        policy.setPolicyErpFlag(obj.optInt("policyErpFlag"));
                        // Erp保单的Id
                        policy.setPolicyErpId(obj.optString("policyErpId"));

                        CompanyBean company = new CompanyBean();
                        company.setId(obj.optString("brandId"));
                        company.setShortName(obj.optString("brandName"));
                        company.setName(obj.optString("brandName"));
                        company.setLogoUrl(obj.optString("logoUrl"));
                        policy.setCompany(company);

                        RiskBean risk = new RiskBean();
                        risk.setId(obj.optString("insuranceCode"));
                        risk.setShortName(obj.optString("insuranceName"));
                        risk.setName(obj.optString("insuranceName"));
                        policy.setRisk(risk);

                        List<Attachment> attachmentList = new ArrayList<>();
                        JSONArray attachArr = obj.getJSONArray("listPolicyAttachment");
                        if (attachArr != null) {
                            for (int j = 0; j < attachArr.length(); j++) {
                                JSONObject attachObj = attachArr.getJSONObject(j);
                                Attachment attachment = new Attachment();
                                attachment.setId(attachObj.optString("id"));
                                attachment.setName(attachObj.optString("attachmentName"));
                                attachment.setUrl(attachObj.optString("attachmentPath"));

                                attachmentList.add(attachment);
                            }
                        }
                        policy.setAttachments(attachmentList);
                        policyList.add(policy);
                    }
                }
                return policyList;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取品牌列表
     *
     * @param result
     * @return
     */
    public static List<CompanyBean> getResponse_CompanyList(String result) {
        try {
            JSONObject resultObj = new JSONObject(result);
            JSONArray dataArr = resultObj.getJSONArray("data");

            List<CompanyBean> companyList = new ArrayList<>();
            int length = dataArr.length();

            for (int i = 0; i < length; i++) {
                JSONObject obj = dataArr.getJSONObject(i);
                CompanyBean c = new CompanyBean();
                c.setId(obj.optString("brandId"));
                c.setName(obj.optString("name"));
                c.setShortName(obj.optString("shortName"));
                c.setLogoUrl(obj.optString("minLogo"));
                companyList.add(c);
            }
            return companyList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * CreatePushToken 创建推送Token
     *
     * @param result
     * @return
     */
    public static PushTokenResult getResponse_PushToken(String result) {
        try {
            JSONObject resultObj = new JSONObject(result);
            JSONObject dataObj = resultObj.getJSONObject("data");
            PushTokenResult pushTokenResult = new PushTokenResult();
            pushTokenResult.pushToken = dataObj.optString("pushToken");
            pushTokenResult.agentId = dataObj.optString("agentId");
            return pushTokenResult;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


}
